package com.hu.zhcc.shiro.service.Appointment;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hu.zhcc.common.entity.Page;
import com.hu.zhcc.shiro.dao.AReservationMapper;
import com.hu.zhcc.shiro.dao.MedicalExaminationReportDAO;
import com.hu.zhcc.shiro.entity.Appointment.AReservation;
import com.hu.zhcc.shiro.entity.bo.MedicalExaminationReport;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ReserServiceImpl implements ReserService {

    @Autowired
    private AReservationMapper reservationMapper;
    @Autowired
    private MedicalExaminationReportDAO medicalExaminationReportDAO;

    @Override
    public Page<AReservation> selectAReserAll(Map<String, String> map, String value) {
        Integer offset = Integer.valueOf(map.get("offset"));
        Integer limit = Integer.valueOf(map.get("limit"));
        PageHelper.startPage(offset, limit);
        List<AReservation> reservations = reservationMapper.selectAReserAll(value);
        PageInfo<AReservation> pageInfo = new PageInfo<>(reservations);
        return new Page<AReservation>(pageInfo.getTotal(), reservations);
    }

    @Override
    public int insertSelective(AReservation record) {
        return reservationMapper.insertSelective(record);
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return reservationMapper.deleteByPrimaryKey(id);
    }

    @Override
    public AReservation beforeUpdateById(int id) {
        return reservationMapper.beforeUpdateById(id);
    }

    @Override
    public int updateByPrimaryKeySelective(AReservation record) {
        return reservationMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateStateByid(int id) {
        return reservationMapper.updateStateByid(id);
    }

    @Override
    public AReservation selectByPrimaryKey(Integer id) {
        return reservationMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<AReservation> findAllAReserByFileNumber(String fileNumber) {
        List<AReservation> list = new ArrayList<>();
        List<AReservation> list2 = reservationMapper.selectAReserAll("");
        for (AReservation aReservation : list2) {
            if (aReservation.getTionNumber().equals(fileNumber)) {
                list.add(aReservation);
            }
        }

        return list;
    }

    @Override
    public List<AReservation> findAllAReserByPhoneNumberAndState1(String phone) {
        List<AReservation> list = new ArrayList<>();
        List<AReservation> allAReserByPhoneNumber = reservationMapper.findAllAReserByPhoneNumber(phone);
        for (AReservation aReservation : allAReserByPhoneNumber) {
            if (aReservation.getTionState() == 1) {
                list.add(aReservation);
            }
        }
        return list;
    }

    @Override
    public List<AReservation> findAllAReserByPhoneNumber(String phone) {
        return reservationMapper.findAllAReserByPhoneNumber(phone);
    }

    @Override
    public int updateConfirmByid(int id) {
        AReservation aReservation = reservationMapper.selectByPrimaryKey(id);
        System.out.println(aReservation);

        MedicalExaminationReport medicalExaminationReport = new MedicalExaminationReport();
        medicalExaminationReport.setName(aReservation.getTionName());
        medicalExaminationReport.setFileNumber(aReservation.getTionNumber());
        medicalExaminationReport.setReportTime(aReservation.getTionApptime());
        medicalExaminationReport.setPhoneNumber(aReservation.getTionPhone());
        String s = medicalExaminationReportDAO.tc(aReservation.getId());
        System.out.println(s);
        medicalExaminationReport.setSetMeal(s);
        medicalExaminationReport.setStatus("报告未上传");
        medicalExaminationReport.setReport("未上传");
        medicalExaminationReportDAO.insertSelective(medicalExaminationReport);
        return reservationMapper.updateConfirmByid(id);
    }

    @Override
    public List<AReservation> findStateByOne() {
        return reservationMapper.findStateByOne();
    }
}
